'use strict'

const config = require('./config')

let url_preg = /^https?\:\/\//

async function req (options) {

  if (!url_preg.test(options.url)) {
    options.url = `${config.host}${options.url[0] === '/' ? '' : '/'}${options.url}`
  }

  let ret = await new Promise((rv, rj) => {
    options.success = res => {
      rv({
        status: res.status,
        data: res.data,
        ok: res.status >= 400 ? false : true,
        header: res.header
      })
    }

    options.fail = err => {
      rj(err)
    }

    wx.request(options)
  })

  return ret
}

module.exports = req
